package com.zheng.durid.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.zheng.durid.config.DruidSourceConfig;
import com.zheng.durid.context.SpringContextUtil;
import com.zheng.zk.Curator;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *  @author: zheng
 *  @Date: 2020-07-02 23:44
 *  @Description:
 */
@Controller
public class TestController {
    private CuratorFramework curatorFramework= Curator.getCuratorFramework();
//    @Autowired
//    private SpringContextUtil springContextUtil;
    @RequestMapping("/test01")
    @ResponseBody
    public String changeA() throws Exception {
       curatorFramework.setData().forPath("/mysqlConfig","jdbc:mysql://localhost:3306/zk01,root,123456".getBytes());
       return new String(curatorFramework.getData().forPath("/mysqlConfig"));
    }

    @RequestMapping("/test02")
    @ResponseBody
    public String changeB() throws Exception {
        curatorFramework.setData().forPath("/mysqlConfig","jdbc:mysql://localhost:3306/zk02,root,123456".getBytes());
        return new String(curatorFramework.getData().forPath("/mysqlConfig"));
    }


    @RequestMapping("/select")
    @ResponseBody
    public String select() throws SQLException {
        DruidDataSource dataSource = DruidSourceConfig.getInstance();
        Connection connection= dataSource.getConnection();
        String sql = "select * from test";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();

        String result = null;
        while (resultSet.next()) {
            result = resultSet.getString("name");
        }

        resultSet.close();
        preparedStatement.close();
        connection.close();
        return result;
    }
}
